package com.jzg.jcptadmin.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.MD5Utils;
import com.jzg.jcptadmin.adpter.StyleCategoryAdapter;
import com.jzg.jcptadmin.app.ActivityHelp;
import com.jzg.jcptadmin.app.AppManager;
import com.jzg.jcptadmin.base.BaseActivity;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.Make;
import com.jzg.jcptadmin.data.vo.Model;
import com.jzg.jcptadmin.data.vo.Style;
import com.jzg.jcptadmin.presenter.StylePresenter;
import com.jzg.jcptadmin.viewinterface.StyleInterface;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by wangyd on 15/12/24.
 */
public class StyleActivity extends BaseActivity implements StyleInterface, View.OnClickListener, AdapterView.OnItemClickListener {
    @Bind(R.id.title_return)
    ImageView title_return;
    @Bind(R.id.title_content)
    TextView title_content;
    @Bind(R.id.style_list)
    ListView style_list;
    @Bind(R.id.loading)
    ViewStub loading;

    private Make make;
    private Model model;
    private ArrayList<Style> styles;
    private ArrayList<String> groupNames;
    private StyleCategoryAdapter styleCategoryAdapter;

    private StylePresenter stylePresenter;

    private Context context = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.style_layout);
        ButterKnife.bind(this);
        initView();
        model = getIntent().getParcelableExtra(ActivityHelp.Model);
        make = getIntent().getParcelableExtra(ActivityHelp.Make);
        stylePresenter = new StylePresenter(DataManager.getInstance());
        stylePresenter.attachView(this);
//        Logger.i(String.valueOf(model.getId()));
        stylePresenter.loadStyle();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        if (stylePresenter != null)
            stylePresenter.detachView();
    }

    private void initView() {
        title_content.setText(getResources().getString(R.string.style));
        loading.setVisibility(View.VISIBLE);
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    @OnClick({R.id.title_return})
    public void onClick(View v) {
        if (v.getId() == R.id.title_return)
            finish();
    }

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p/>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent   The AdapterView where the click happened.
     * @param view     The view within the AdapterView that was clicked (this
     *                 will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id       The row id of the item that was clicked.
     */
    @Override
    @OnItemClick(R.id.style_list)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Logger.i(styles.get(position).getFullName() + ":" + styles.get(position).getId());
//        stylePresenter.sendCar(appContext, styles.get(position));
        Style style = styles.get(position);
        style.setMakeId(make.getMakeId());
        style.setModelId(model.getId());
        sendCarSelectInfoBroadcase(style);
        finish();
        AppManager.getAppManager().finishActivity(BrandActivity.class);
    }

    private void sendCarSelectInfoBroadcase(Style style) {
        Intent intent = new Intent();
        intent.setAction(Constant.CAR_SELECT_BROADCAST);
        intent.putExtra(Constant.MAKE, make);
        intent.putExtra(Constant.MODEL, model);
        intent.putExtra(Constant.STYLE, (Serializable) style);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    /**
     * 显示车型列表
     */
    @Override
    public void showStyle() {
        loading.setVisibility(View.GONE);
        styleCategoryAdapter = new StyleCategoryAdapter(this, styles, groupNames);
        style_list.setAdapter(styleCategoryAdapter);
    }

    /**
     * 设置车型数据
     *
     * @param styles 车型数据
     */
    @Override
    public void setStyles(ArrayList<Style> styles) {
        this.styles = styles;
    }

    /**
     * 设置车型分组数据
     *
     * @param groupNames 车型分组名称--YEAR
     */
    @Override
    public void setStylesGroupNames(ArrayList<String> groupNames) {
        this.groupNames = groupNames;
    }

    /**
     * 获取默认字体颜色
     *
     * @return 字体颜色
     */
    @Override
    public int getDefaultFontColor() {
        return getResources().getColor(R.color.grey3);
    }

    /**
     * 获取样式列表参数
     *
     * @return
     */
    @Override
    public Map<String, String> getStyleParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("tokenid", "6");
        params.put("ModelId", String.valueOf(model.getId()));
        params.put("op", "GetStyle");
        params.put("InSale", "1");
        params.put("userId", appContext.getUser().getUserId()+"");
        Map<String, Object> signMaps = new HashMap<>();
        signMaps.putAll(params);
//        params.put("sign", MD5Utils.getMD5Sign(signMaps, "2CB3147B-D93C-964B-47AE-EEE448C84E3C"));
        params.put("sign", MD5Utils.getMD5Sign(signMaps, "3CEA5FCD-451F-4B97-87B6-A2C6C7C8A5FD"));
        return params;
    }

    /**
     * 显示错误信息
     *
     * @param error 错误信息
     */
    @Override
    public void showError(String error) {
        loading.setVisibility(View.GONE);
        Toast.makeText(this, Constant.errr_net, Toast.LENGTH_LONG).show();
    }
}
